﻿package tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Money {
	private static final String[] bigNumber = { "零", "壹", "贰", "叁", "肆", "伍",
			"陆", "柒", "捌", "玖" };
	private static final String[] bigUnit = { "元", "拾", "佰", "仟", "万", "拾",
			"佰", "仟", "亿", "拾", "佰", "仟", "万" };
//	private static final char[] units = new char[] { '元', '拾', '佰', '仟', '万',
//			'拾', '佰', '仟', '亿' };
//	private static final char[] data = new char[] { '零', '壹', '贰', '叁', '肆',
//			'伍', '陆', '柒', '捌', '玖' };

	public static void main(String[] args) throws IOException {
		// System.out.println(convert(123));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入([bye]退出系统):");
		String money ="";
		while (true) {
			money = br.readLine();
			if (money.equals("bye")) {
				System.out.println("感谢使用！再见！");
				break;
			}
			toBigMoney(money);
		}
	}

	public static void toBigMoney(String money) {
		int len = money.length();
		
//		if (money==null||money.equals("")||(len > 13) || !(money.replaceAll("[^0-9]+", "1").equals(money)) || (money.charAt(0) == '0')) {
		/*正则就是牛b*/
		if(!money.matches("^[1-9]\\d{0,12}$")){
			System.out.println("请输入正确的金额(小于13位的数字)");
			return;
		}
		StringBuffer bigmoney = new StringBuffer();
		for (int i = 0; i < len; i++) {
			int num = Integer.parseInt(String.valueOf(money.charAt(i)));
			bigmoney.append(bigNumber[num] + bigUnit[len - i - 1]);
		}
		String bmoney = String.valueOf(bigmoney).replaceAll("零[拾佰仟]", "零")
				.replaceAll("零[零]+", "零").replaceAll("零元", "元")
				.replaceAll("零万", "万").replaceAll("零亿", "亿")
				.replaceAll("亿万", "亿");
		System.out.println("小写：" + money + "\n大写：" + bmoney);
	}

	// public static String convert(int money) {
	// StringBuffer sbf = new StringBuffer();
	// int unit = 0;
	// while (money != 0) {
	// sbf.insert(0, units[unit++]);
	// int number = money % 10;
	// sbf.insert(0, data[number]);
	// money /= 10;
	// }
	// return sbf.toString().replaceAll("零[拾佰仟]", "零")
	// .replaceAll("零[零]+", "零").replaceAll("零元", "元")
	// .replaceAll("零万", "万").replaceAll("零亿", "亿")
	// .replaceAll("亿万", "亿");
	// }
}
